package com.aivin.lib_base.tool;

import android.content.Context;
import android.os.Environment;
import android.os.Handler;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;


public class FileUtil {
    public static final int SUCCESS = 1;
    public static final int FAILED = 0;
    private Context context;
    private volatile boolean isSuccess;
    private String errorStr;
    private Handler handler;

    public FileUtil(Context context , Handler handler  ) {
        this.context = context;
        this.handler = handler;
    }

    public void copyAssetsToSD(final String srcPath, final String sdPath) {
        new Thread( new Runnable() {
            @Override
            public void run() {
                copyAssetsToDst(context, srcPath, sdPath);
                if (isSuccess){
                    handler.obtainMessage(SUCCESS).sendToTarget();
                } else{
                    handler.obtainMessage(FAILED, errorStr).sendToTarget();
                }
            }
        }).start();
    }



    private void copyAssetsToDst(Context context, String srcPath, String dstPath) {
        try {
            String [] fileNames = context.getAssets().list(srcPath);

            if(fileNames==null){
                throw new  Exception("fileNames is null");
            }

            if (fileNames.length > 0) {
                File file = new File(  dstPath);
                if (!file.exists()) {
                    file.mkdirs();
                }
                for (String fileName : fileNames) {
                    String srctemp = srcPath + File.separator + fileName;
                    String  destTemp = dstPath + File.separator + fileName;
                    copyAssetsToDst(context,srctemp ,destTemp );
                }
            } else {
                File outFile = new File(  dstPath);
                if(!outFile.exists()){
                    InputStream is = context.getAssets().open(srcPath);
                    FileOutputStream fos = new FileOutputStream(outFile);
                    byte[] buffer = new byte[1024];
                    int byteCount;
                    while ((byteCount = is.read(buffer)) != -1) {
                        fos.write(buffer, 0, byteCount);
                    }
                    fos.flush();
                    is.close();
                    fos.close();
                }

            }
            isSuccess = true;
        } catch (Exception e) {
            e.printStackTrace();
            errorStr = e.getMessage();
            isSuccess = false;
        }
    }



    public  static String getWkReaderRoot() {
        String lastPath  ;
        boolean hasSd = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) ;
        if (hasSd) {
            lastPath = Environment.getExternalStorageDirectory() + File.separator+ "WkReader";
        } else {
            lastPath = Environment.getDataDirectory() + File.separator +"WkReader";
        }

        File file = new File(lastPath);
        if (!file.exists()) {
            file.mkdirs();
        }
        return lastPath;
    }

}


